package com.ruoyi.business.controller;

import com.alibaba.fastjson.JSON;
import com.ruoyi.business.domain.CarPackageAudit;
import com.ruoyi.business.domain.ServiceItem;
import com.ruoyi.business.service.ICarPackageAuditService;
import com.ruoyi.business.service.IProcessService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * 审核列表Controller
 * 
 * @author wolfcode
 * @date 2021-07-22
 */
@Controller
@RequestMapping("/business/carPackageAudit")
public class CarPackageAuditController extends BaseController
{
    private String prefix = "business/carPackageAudit";
    @Autowired
    private IProcessService processService;

    @Autowired
    private ICarPackageAuditService carPackageAuditService;

    @RequiresPermissions("business:carPackageAudit:view")
    @GetMapping()
    public String carPackageAudit()
    {
        return prefix + "/carPackageAudit";
    }

    /**
     * 查询审核列表列表
     */
    @RequiresPermissions("business:carPackageAudit:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CarPackageAudit carPackageAudit)
    {
        startPage();
        List<CarPackageAudit> list = carPackageAuditService.selectCarPackageAuditList(carPackageAudit);
        return getDataTable(list);
    }

    /**
     * 导出审核列表列表
     */
    @RequiresPermissions("business:carPackageAudit:export")
    @Log(title = "审核列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CarPackageAudit carPackageAudit)
    {
        List<CarPackageAudit> list = carPackageAuditService.selectCarPackageAuditList(carPackageAudit);
        ExcelUtil<CarPackageAudit> util = new ExcelUtil<CarPackageAudit>(CarPackageAudit.class);
        return util.exportExcel(list, "审核列表数据");
    }

    /**
     * 新增审核列表
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存审核列表
     */
    @RequiresPermissions("business:carPackageAudit:add")
    @Log(title = "审核列表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CarPackageAudit carPackageAudit)
    {
        return toAjax(carPackageAuditService.insertCarPackageAudit(carPackageAudit));
    }

    /**
     * 修改审核列表
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        CarPackageAudit carPackageAudit = carPackageAuditService.selectCarPackageAuditById(id);
        mmap.put("carPackageAudit", carPackageAudit);
        return prefix + "/edit";
    }

    /**
     * 修改保存审核列表
     */
    @RequiresPermissions("business:carPackageAudit:edit")
    @Log(title = "审核列表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CarPackageAudit carPackageAudit)
    {
        return toAjax(carPackageAuditService.updateCarPackageAudit(carPackageAudit));
    }

    /**
     * 删除审核列表
     */
    @RequiresPermissions("business:carPackageAudit:remove")
    @Log(title = "审核列表", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(carPackageAuditService.deleteCarPackageAuditByIds(ids));
    }
    @RequestMapping("/processImg")
    @ResponseBody
    public void processImg(String instanceId, HttpServletResponse response) throws IOException {
        InputStream inputStream = processService.getProcessImgByInstanceId(instanceId);
        IOUtils.copy(inputStream,response.getOutputStream());
    }
    @RequestMapping("/cancelApply")
    @ResponseBody
    public AjaxResult cancelApply(String instanceId,Long carPackageAuditId){
        carPackageAuditService.cancelApply(instanceId,carPackageAuditId);
        return AjaxResult.success("撤销成功");
    }
    @RequestMapping("/todoPage")
    public String todoPage(){
        return prefix+"/todoPage";
    }
    /**
     * 查询审核列表列表
     */
    @PostMapping("/todoList")
    @ResponseBody
    public TableDataInfo todoList(CarPackageAudit carPackageAudit)
    {
        List<CarPackageAudit> list = carPackageAuditService.selectTodoList(carPackageAudit);
        return getDataTable(list);
    }
    @RequestMapping("/auditDialogPage")
    public String auditDialogPage(String taskId,ModelMap mmap){
        mmap.put("taskId",taskId);
        return prefix+"/taskVerify";
    }

    @RequestMapping("/complete/{taskId}")
    @ResponseBody
    public AjaxResult complete(@PathVariable("taskId") String taskId, Boolean auditStatus,String comment){
        carPackageAuditService.complete(taskId,auditStatus,comment);
        return AjaxResult.success();
    }
    @RequestMapping("/serviceItemsEditPage/{carPackageAuditId}")
    public String serviceItemsEditPage(@PathVariable("carPackageAuditId")Long carPackageAuditId,ModelMap mmap){
        CarPackageAudit audit = carPackageAuditService.selectCarPackageAuditById(carPackageAuditId);
        ServiceItem item = JSON.parseObject(audit.getServiceItemInfo(),ServiceItem.class);
        mmap.put("serviceItem",item);
        mmap.put("carPackageAuditId",carPackageAuditId);
        return prefix+"/serviceItemsEditPage";
    }
    @RequestMapping("/servieItemsUpdate")
    @ResponseBody
    public AjaxResult servieItemsUpdate(Long carPackageAuditId,ServiceItem serviceItem){
        carPackageAuditService.serviceItemsUpdate(carPackageAuditId,serviceItem);
        return success();
    }
}
